﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LevelController : MonoBehaviour {

	public static int level = 1;
	//public GameObject[] levelObject; 
	public Text centertext;
    // 单例实例
    public static LevelController Instance { get; private set; }

    // 确保在游戏启动时创建唯一的实例
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);  // 如果已经存在实例，则销毁新的实例
        }
    }
    // Use this for initialization
    void Start () {

        UpdateLevel(1);
    }

    //更新下一关数据
    public void UpdateLevel(int num) 
    {
        PlayerPrefs.SetInt("x", num);
        foreach (Transform child in transform)
        {
            if (child.name.StartsWith("ball"))
            {
                Destroy(child.gameObject);  // 销毁该子物体
            }
        }
        // 获取当前的关卡等级
        level = PlayerPrefs.GetInt("x");
        // 获取UI中名为"Text"的Text组件
        centertext = GameObject.Find("Text").GetComponent<Text>();
        centertext.text = level.ToString();

        if (level == 1)
        {
            transform.GetComponent<instantiateball>().number = 5;
            transform.GetComponent<ballTurn>().rotateStatus = 1;
            transform.GetComponent<ballTurn>().velocity = 300;// 关卡1时，设置球的速度
        }
        else if (level == 2)
        {
            transform.GetComponent<instantiateball>().number = 5;
            transform.GetComponent<ballTurn>().rotateStatus = 2;// 关卡2时，设置球的旋转状态
            transform.GetComponent<ballTurn>().velocity = 300;// 关卡1时，设置球的速度
        }
        else if (level == 3)
        {
            transform.GetComponent<instantiateball>().number = 99;// 关卡3时，设置球的数量为99
            transform.GetComponent<ballTurn>().velocity = 300;// 关卡1时，设置球的速度
        }
    }

    // Update is called once per frame
    void Update () {
	



	}
	public void onWin(){
		if (level == 3) {
			Debug.Log("重新开始");
            UpdateLevel(1);// 加载第一个场景（重新开始游戏）
        } else {
			level ++;
            UpdateLevel(level);
        }
        instantiateball.Instance.nemberswitch = true;
	}
}
